<?php

/* Print form input table */

echo "<table class=\"form-input\">\n";
        for ($i=0; $i < $fields; $i++) {
                if ($i % 2 == 0) {
                        if ($i != 0) {
                                echo "</tr>\n";
                        }
                        echo "<tr>";
                }
                $name  = mysql_field_name($result, $i);
                echo "<td";
		if ($name == $error_field) {
			echo " class=\"error-field\" ";
		}
		/* Print table field */
		echo ">" . $name;
		$flags = mysql_field_flags($result, $i);
		if (strpos($flags, "not_null")!==false && $i!=0){
			echo "*";
			$not_null_exist = true;
		}
		if ((strpos($flags, "unique_key")!==false || strpos($flags, "primary_key")!==false) && $i!=0 ) {
			echo "`";
			$unique_exist = true;
		}
		/* Print options in field that is a foriegn key  */
		if (strpos($flags, "multiple_key")!==false) {
			echo "~";
			$mult_key_exist = true;
			$sql = "SELECT " . $ddl[$name]['REF_COLUMN_NAME'] . " FROM " . $ddl[$name]['REF_TABLE_NAME'];
			$result2 = mysql_query($sql);
			if ($result2) {
				echo "</td><td>";
				echo "<select name='aed_" . $name . "'>";
				if (($edit_selected!='' || 
					$_POST['Submit']=='Form Input' || 
					$_POST['Submit']=='Form Edit') 
/*&&
(mysql_num_rows(mysql_query("SELECT " . $ddl[$name]['REF_COLUMN_NAME']  . " FROM " .$ddl[$name]['REF_TABLE_NAME']. " WHERE " . $ddl[$name]['REF_COLUMN_NAME'] . "='" . $plain[$i] . "'")))*/
) {
					/* Print data value  */
					echo "<option>" . htmlspecialchars($plain[$i]) . "</option>";
				}
				if  (strpos($flags, "not_null")===false){
					echo "<option></option>";
				}
				while ($row2 = mysql_fetch_array($result2)) {
					echo "<option>" . $row2[$ddl[$name]['REF_COLUMN_NAME']] . "</option>";
				}
				echo "</select>";
				echo "</td>";
				continue;
			}
		}
		echo "</td>\n<td";
               	if ($i % 2 == 0) {
               	        echo " class='first-col'";
               	}
               	echo ">";
		if ($i==0) {
			echo htmlspecialchars($plain[$i]) . "</td>\n";
			echo "<input type='hidden' name=\"aed_" . $name . "\" ".
				"value=\"". htmlspecialchars($plain[$i]) . "\" />";
		} else {
			echo "<input type='text' maxlength='".mysql_field_len($result, $i)."' name=\"aed_" . $name . "\" ";
               	if ($edit_selected!='' || $_POST['Submit']=='Form Input' || $_POST['Submit']=='Form Edit') {
			echo "value = \"" . htmlspecialchars($plain[$i]) . "\" /></td>\n";
	               	}
  	             	else {
 	                      	echo "/></td>\n";
  	             	}	
		}
        }
        if ($field % 2 == 0) {
                echo "</tr>\n";
        } else echo "<td></td><td></td><tr>\n";
                echo "</table>\n";



?>
